<?php

class Cookie extends Base {

  /** @var string The name for the current cookie. */
  private $name;

  /**
   * Store the cookie name in private.
   * @param string $name The name of the cookie.
   * @link http://fr2.php.net/manual/en/function.is-string.php
   * @link http://fr.php.net/manual/en/function.empty.php
   */
  public function __construct($name = "mycookie") {
    if (!empty($name))
      if (is_string($name))
        $this->name = $name;
      else
        throw new IllegalArgumentException("\$name parameter should be a string");
    else
      throw new IllegalArgumentException("\$name parameter string is empty");
  }

  /**
   * Defines a cookie to be sent along with the rest of the HTTP headers.
   * @param string $name The name of the cookie.
   * @param string $value The value of the cookie.
   * @param integer $expire The time the cookie expires.
   * @param string $path The path on the server in which the cookie will be available on.
   * @param string $domain The domain that the cookie is available.
   * @param boolan $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
   * @param boolean $httponly When TRUE the cookie will be made accessible only through the HTTP protocol.
   * @return boolean Returns TRUE on succes or FALSE on failure.
   * @link http://fr2.php.net/manual/en/function.setcookie.php
   */
  public function add($name, $value, $expire = 0, $path = "", $domain = "", $secure = FALSE, $httponly = FALSE) {

    if (is_string($name))
      if (is_string($value))
        if (is_int($expire))
          if (is_string($path))
            if (is_string($domain))
              if (is_bool($secure))
                if (is_bool($httponly))
                  return setcookie($this->name . "[" . $name . "]", $value, $expire, $path, $domain, $secure, $httponly);
                else
                  throw new IllegalArgumentException("\$httponly parameter should be a boolean");
              else
                throw new IllegalArgumentException("\$secure parameter should be a boolean");
            else
              throw new IllegalArgumentException("\$domain parameter should be a string");
          else
            throw new IllegalArgumentException("\$path parameter should be a string");
        else
          throw new IllegalArgumentException("\$expire parameter should be an integer");
      else
        throw new IllegalArgumentException("\$value parameter should be a string");
    else {
      throw new IllegalArgumentException("\$name parameter should be a string");
    }
  }

  /**
   * Delete the cookie. If $name string parameter is provided, the function delete the sub cookie indicated by name.
   * @param string $name The name of the cookie.
   * @return boolean Returns TRUE on succes or FALSE on failure.
   * @link http://fr.php.net/manual/en/function.is-string.php
   * @link http://fr.php.net/manual/en/function.empty.php
   * @link http://fr.php.net/manual/en/function.setcookie.php
   */
  public function delete($name = "") {
    if (is_string($name))
      if (empty($name))
        return setcookie($this->name, "", 1);
      else
        return setcookie($this->name . "[" . $name . "]", "", 1);
    else
      throw new IllegalArgumentException("\$name parameter should be a string");
  }

  /**
   * Defines if the cookie exists or not.
   * @param string $name The name of the cookie.
   * @return boolean Returns TRUE if the cookie exists or FALSE if not.
   * @link http://fr.php.net/manual/en/function.is-string.php
   * @link http://fr2.php.net/manual/en/function.isset.php
   */
  public function exists($name = "") {
    if (is_string($name))
      if (empty($name))
        return isset($_COOKIE[$this->name]);
      else
        return isset($_COOKIE[$this->name][$name]);
    else
      throw new IllegalArgumentException("\$name parameter should be a string");
  }

  /**
   * Returns the PHP session ID for the cookie.
   * @return string Returns the PHP session ID.
   */
  public function getSessionId() {
    return $_COOKIE['PHPSESSID'];
  }

  /**
   * Set a value for the $name cookie. If the cookie does'nt exists, it will be created.
   * @param string $name The name of the cookie.
   * @param string $value The value for the cookie.
   * @return boolean Returns TRUE on succes or FALSE on failure.
   * @link http://fr.php.net/manual/en/function.is-string.php
   * @link http://fr2.php.net/manual/en/function.setcookie.php
   */
  public function setValue($value, $name = "") {
    if (is_string($name))
      if (is_string($value))
        if (!empty($name))
          return setcookie($this->name . "[" . $name . "]", $value);
        else
          return setcookie($this->name, $value);
      else
        throw new IllegalArgumentException("\$value parameter should be a string");
    else
      throw new IllegalArgumentException("\$name parameter should be a string");
  }

  /**
   * Get the value of the cookie.
   * @param string $name The name of the cookie.
   * @return string Returns the value of the $name cookie.
   * @link http://fr.php.net/manual/en/function.is-string.php
   * @link http://fr2.php.net/manual/en/function.empty.php
   */
  public function getValue($name = "") {
    if (is_string($name)) {
      if (empty($name)) {
        return $_COOKIE[$this->name];
      } else {
        return $_COOKIE[$this->name][$name];
      }
    } else {
      throw new IllegalArgumentException("\$name parameter should be a string");
    }
  }

}

?>
